package com.jandan.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.jandan.logic.JWordzFacade;
import com.jandan.ui.model.CnAndSentence;
import com.jandan.ui.model.LearnedWord;
import com.jandan.ui.model.Word;

/**
 * 
 * 用于处理获取已学单词的ajax请求，返回json格式数据，请求路径在json-servlet.xml中配置，关于所使用的json处理包，参见相关文档
 * 这个类用户单词学习的客户端，可以不用管先
 * @author Gong Yong
 * @version 1.0
 *
 */
public class LearnedWordBrowserController implements Controller {
	private JWordzFacade jwordz;

	public void setJwordz(JWordzFacade jwordz) {
		this.jwordz = jwordz;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		long lessonID = Long.parseLong(request.getParameter("lessonID"));
		int libNumber = Integer.parseInt(request.getParameter("libNumber"));
		
		List<LearnedWord> words = jwordz.getLearnedWordListByLessonIDAndLibNumber(lessonID,libNumber,start,limit);
		
		int count = jwordz.getLearnedWordCountByLessonIDAndLibNumber(lessonID,libNumber);
		
		Map model = new HashMap();
		model.put("totalCount", count);
		ArrayList list = new ArrayList();

		Iterator<LearnedWord> it = words.iterator();
		LearnedWord learnedWord = null;
		Word word = null;
		Map map = null;
		StringBuffer wordCn = null;
		StringBuffer details = null;
		List<CnAndSentence> csList = null;
		CnAndSentence cs = null;
		String c = null;
		String s = null;
		while (it.hasNext()) {
			learnedWord = it.next();
			word = learnedWord.getWord();
			map = new HashMap();
			map.put("learnedWordID", learnedWord.getWordID());
			map.put("wordEn", word.getWordEn());
			map.put("phonetics", word.getPhonetics());

			wordCn = new StringBuffer();
			details = new StringBuffer();
			csList = word.getCnAndSentenceList();

			for (int i = 0, n = csList.size(); i < n; i++) {
				cs = csList.get(i);
				if (cs != null) {
					wordCn.append(cs.getCn());
					wordCn.append(";");

					c = cs.getCn();
					s = cs.getSentence();

					if (s != null && s.trim().length() > 0
							&& !s.trim().equalsIgnoreCase("none")) {
						details.append(c);
						wordCn.append("|");
						wordCn.append(s);
						wordCn.append(i != n - 1 ? "|" : "");
					}
				}
			}

			map.put("wordCn", wordCn.toString());
			map.put("details", details.toString());
			map.put("learnedTimes",learnedWord.getLearnedTimes());
			map.put("degree", learnedWord.getDegree());
			map.put("startLearnedDate", learnedWord.getStartLearnedDate());
			map.put("latestLearnedDate", learnedWord.getLatestLearnedDate());
			list.add(map);
		}
		
		model.put("learnedWords", list);
		return new ModelAndView("jsonView", model);
	}

}
